const Service = require('egg').Service;

class RoleService extends Service {
    // 创建角色
    async createRole(data) {
        return new Promise(async (resolve, reject) => {
            this.ctx.model.Role({
                ...data
            }).save((err, data) => {
                if (err) {
                    console.log(err);
                    reject(err);
                    return
                }
                resolve(data)
            });
        })
    }

    // 查询角色
    async findRole(params = {}) {
        const pageSize = params && parseInt(params.pageSize) || 20; // 使用分页
        const page = params && parseInt(params.page) || 1;
        delete params.pageSize;
        delete params.page;
        return new Promise((resolve, reject) => {
            this.ctx.model.Role.find(params).limit(pageSize).skip(pageSize * (page - 1)).sort('-createDate').lean().exec((err, docs) => {
                if (err) {
                    console.log(err);
                    reject(err);
                } else {
                    resolve(docs);
                }
            })
        })
    }

    // 查询单个角色
    async findOneRole(params = {}) {

        return new Promise((resolve, reject) => {
            this.ctx.model.Role.findOne(params).lean().exec((err, doc) => {
                if (err) {
                    console.log(err);
                    reject(err);
                } else {
                    resolve(doc);
                }
            });
        })
    }

    // 更新单个角色以及权限
    async updateOneRole(condition = {}, doc) {
        return new Promise((resolve, reject) => {
            // .update(condition,doc,[options],[callback]);
            this.ctx.model.Role.updateOne(condition, { ...doc }).lean().exec(async (err, result) => {
                if (err) {
                    console.log(err);
                    reject(err);
                } else {
                    // 先获取当前角色
                    const role = await this.findOneRole(condition);
                    // 清除角色的权限
                    const p = [role.name];
                    await this.ctx.app.enforcer.removeFilteredPolicy(0, ...p);

                    // 设置新权限
                    if (doc.power && doc.power.length) {

                        this.ctx.model.Power.find({ _id: doc.power }).lean().exec(async (error, powers) => {
                            if (error) {
                                console.log(error);
                                reject(error);
                            } else {
                                if (powers.length) {
                                    try {
                                        for (let power of powers) {
                                            await this.ctx.app.enforcer.addPolicy(role.name, power.router, power.method);
                                        }
                                    } catch (e) {
                                        reject(e);
                                    }
                                }
                                resolve(result);
                            }
                        });
                    } else {
                        resolve(result);
                    }
                }
            });
        })
    }

    // 删除角色
    async deleteRole(id) {
        return new Promise((resolve, reject) => {
            this.ctx.model.Role.remove({
                _id: id
            }).lean().exec((err, docs) => {
                if (err) {
                    console.log(err);
                    reject(err);
                } else {
                    resolve(docs);
                }
            });
        })
    }
}

module.exports = RoleService;